package org.glow.changgou.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.user.dao.OauthClientDetailsMapper;
import org.glow.changgou.user.pojo.Cities;
import org.glow.changgou.user.pojo.OauthClientDetails;
import org.glow.changgou.user.service.OauthClientDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:admin
 * @Description:OauthClientDetails业务层接口实现类
 * @Date 2022/10/02 21:15:17
 *****/
@Service
public class OauthClientDetailsServiceImpl extends ServiceImpl<OauthClientDetailsMapper, OauthClientDetails> implements OauthClientDetailsService {

    @Resource
    private OauthClientDetailsMapper oauthClientDetailsMapper;


    /**
     * OauthClientDetails条件+分页查询
     * @param oauthClientDetails 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<OauthClientDetails> findPage(OauthClientDetails oauthClientDetails, int page, int size){
        //分页
        Page<OauthClientDetails> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<OauthClientDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,oauthClientDetails);
        return oauthClientDetailsMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * OauthClientDetails分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<OauthClientDetails> findPage(int page, int size){
        Page<OauthClientDetails> pageInfo = new Page<>(page,size);

        return oauthClientDetailsMapper.selectPage(pageInfo,null);
    }

    /**
     * OauthClientDetails条件查询
     * @param oauthClientDetails
     * @return
     */
    @Override
    public List<OauthClientDetails> findList(OauthClientDetails oauthClientDetails){
        LambdaQueryWrapper<OauthClientDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,oauthClientDetails);
        return oauthClientDetailsMapper.selectList(queryWrapper);
    }


    /**
     * OauthClientDetails构建查询对象
     * @param oauthClientDetails
     * @return
     */
    public LambdaQueryWrapper<OauthClientDetails> getCondition(LambdaQueryWrapper<OauthClientDetails> queryWrapper, OauthClientDetails oauthClientDetails){

        if(oauthClientDetails!=null){
            // 客户端ID，主要用于标识对应的应用
            if(!StringUtils.isEmpty(oauthClientDetails.getClientId())){
//                    criteria.andEqualTo("clientId",oauthClientDetails.getClientId());
                    queryWrapper.eq(OauthClientDetails::getClientId,oauthClientDetails.getClientId());
            }
            // 
            if(!StringUtils.isEmpty(oauthClientDetails.getResourceIds())){
//                    criteria.andEqualTo("resourceIds",oauthClientDetails.getResourceIds());
                    queryWrapper.eq(OauthClientDetails::getResourceIds,oauthClientDetails.getResourceIds());
            }
            // 客户端秘钥，BCryptPasswordEncoder加密算法加密
            if(!StringUtils.isEmpty(oauthClientDetails.getClientSecret())){
//                    criteria.andEqualTo("clientSecret",oauthClientDetails.getClientSecret());
                    queryWrapper.eq(OauthClientDetails::getClientSecret,oauthClientDetails.getClientSecret());
            }
            // 对应的范围
            if(!StringUtils.isEmpty(oauthClientDetails.getScope())){
//                    criteria.andEqualTo("scope",oauthClientDetails.getScope());
                    queryWrapper.eq(OauthClientDetails::getScope,oauthClientDetails.getScope());
            }
            // 认证模式
            if(!StringUtils.isEmpty(oauthClientDetails.getAuthorizedGrantTypes())){
//                    criteria.andEqualTo("authorizedGrantTypes",oauthClientDetails.getAuthorizedGrantTypes());
                    queryWrapper.eq(OauthClientDetails::getAuthorizedGrantTypes,oauthClientDetails.getAuthorizedGrantTypes());
            }
            // 认证后重定向地址
            if(!StringUtils.isEmpty(oauthClientDetails.getWebServerRedirectUri())){
//                    criteria.andEqualTo("webServerRedirectUri",oauthClientDetails.getWebServerRedirectUri());
                    queryWrapper.eq(OauthClientDetails::getWebServerRedirectUri,oauthClientDetails.getWebServerRedirectUri());

            }
            // 
            if(!StringUtils.isEmpty(oauthClientDetails.getAuthorities())){
//                    criteria.andEqualTo("authorities",oauthClientDetails.getAuthorities());
                    queryWrapper.eq(OauthClientDetails::getAuthorities,oauthClientDetails.getAuthorities());
            }
            // 令牌有效期
            if(!StringUtils.isEmpty(oauthClientDetails.getAccessTokenValidity())){
//                    criteria.andEqualTo("accessTokenValidity",oauthClientDetails.getAccessTokenValidity());
                    queryWrapper.eq(OauthClientDetails::getAccessTokenValidity,oauthClientDetails.getAccessTokenValidity());
            }
            // 令牌刷新周期
            if(!StringUtils.isEmpty(oauthClientDetails.getRefreshTokenValidity())){
//                    criteria.andEqualTo("refreshTokenValidity",oauthClientDetails.getRefreshTokenValidity());
                    queryWrapper.eq(OauthClientDetails::getRefreshTokenValidity,oauthClientDetails.getRefreshTokenValidity());
            }
            // 
            if(!StringUtils.isEmpty(oauthClientDetails.getAdditionalInformation())){
//                    criteria.andEqualTo("additionalInformation",oauthClientDetails.getAdditionalInformation());
                    queryWrapper.eq(OauthClientDetails::getAdditionalInformation,oauthClientDetails.getAdditionalInformation());
            }
            // 
            if(!StringUtils.isEmpty(oauthClientDetails.getAutoapprove())){
//                    criteria.andEqualTo("autoapprove",oauthClientDetails.getAutoapprove());
                    queryWrapper.eq(OauthClientDetails::getAutoapprove,oauthClientDetails.getAutoapprove());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        oauthClientDetailsMapper.deleteById(id);
    }

    /**
     * 修改OauthClientDetails
     * @param oauthClientDetails
     */
    @Override
    public void update(OauthClientDetails oauthClientDetails){
        oauthClientDetailsMapper.updateById(oauthClientDetails);
    }

    /**
     * 增加OauthClientDetails
     * @param oauthClientDetails
     */
    @Override
    public void add(OauthClientDetails oauthClientDetails){
        oauthClientDetailsMapper.insert(oauthClientDetails);
    }

    /**
     * 根据ID查询OauthClientDetails
     * @param id
     * @return
     */
    @Override
    public OauthClientDetails findById(String id){
        return  oauthClientDetailsMapper.selectById(id);
    }

    /**
     * 查询OauthClientDetails全部数据
     * @return
     */
    @Override
    public List<OauthClientDetails> findAll() {
        return oauthClientDetailsMapper.selectList(null);
    }
}
